package cn.yikaojiuguo.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.MD5Util;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.BaseLoginActivity;
import cn.yikaojiuguo.student.ui.global.HomeActivity;

public class PassWordInputActivity extends BaseLoginActivity {

    @Bind(R.id.et_password)
    EditText mEt_password;
    @Bind(R.id.btn_login)
    Button mBtn_login;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(), PassWordInputActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_word_input);
        ButterKnife.bind(this);
        initEvents();
    }

    private void initEvents() {
        mBtn_login.setOnClickListener(this);
        mEt_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //关闭键盘
                    Util.closeSoftInput(PassWordInputActivity.this);
                }
            }
        });
        //弹出英文键盘
        mEt_password.requestFocus();
        Util.openSoftInput(mEt_password);
    }

    @Override
    protected String getOperationText() {
        return getResources().getString(R.string.activity_password_input_operation_text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                startActivity(LoginActivity.newIntent());
                finish();
                leftEnter();
                break;
            case R.id.tv_operation:
                startActivity(RetrievePasswordActivity.newIntent());
                finish();
                rightEnter();
                break;
            case R.id.btn_login:
                String password = mEt_password.getText().toString().trim();
                if (password.length() < 6) {
                    ToastUtil.showToast(this, "密码最少6位");
                    return;
                }
                showProgressDialogNotCancel();
                String requestUrl = UrlConstants.LOG_IN + "&mobile=" + YiKaoApplication
                        .getMobileNubmer() + "&pwd=" + MD5Util.encode(password);
                RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        dismissProgressDialog();
                        if(response!=null && response.optBoolean("success")) {
                            Util.closeSoftInput(PassWordInputActivity.this);
                            JSONObject data = response.optJSONObject("data");
                            String userId = data.optString("id");
                            String token = data.optString("token");
                            YiKaoApplication.clearCacheUserInfo();
                            //保存user id和token
                            YiKaoApplication.putUserId(userId);
                            YiKaoApplication.putSecretkey(token);
                            ToastUtil.showToast(PassWordInputActivity.this, "登录成功");
                            startActivity(HomeActivity.newIntent(2));
                            finish();
                        }
                    }
                },null,mTag);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        mIv_back.performClick();
    }
}
